//
//  MKLoadingView.m
//  Jingjing
//
//  Created by Maxwell Rose on 15/11/30.
//  Copyright © 2015年 Mikeshihua. All rights reserved.
//

#import "MKLoadingView.h"

@interface MKLoadingView ()

@property (strong, nonatomic) UIImageView *imageView;
@property (strong, nonatomic) UILabel *infoLabel;

@end

@implementation MKLoadingView

- (instancetype)init{
    if (self = [super init]) {
        [self setUp];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setUp];
    }
    return self;
}

- (void)setUp{
    self.backgroundColor = [UIColor clearColor];
    
    _imageView = [[UIImageView alloc] init];
    //设置动画帧
    _imageView.animationImages=[NSArray arrayWithObjects:
                                [UIImage imageNamed:@"loadpage01"],
                                [UIImage imageNamed:@"loadpage02"],
                                [UIImage imageNamed:@"loadpage03"],
                                [UIImage imageNamed:@"loadpage04"],
                                [UIImage imageNamed:@"loadpage05"],
                                [UIImage imageNamed:@"loadpage06"],
                                [UIImage imageNamed:@"loadpage07"],
                                [UIImage imageNamed:@"loadpage08"],
                                [UIImage imageNamed:@"loadpage09"],
                                nil ];
    
    _infoLabel = [[UILabel alloc]init];
    _infoLabel.textAlignment = NSTextAlignmentCenter;
    _infoLabel.font = [UIFont systemFontOfSize:14];
    _infoLabel.textColor = kColorOrangePressed;
    _infoLabel.text = @"加载中...";
    self.layer.hidden = YES;
    
    [self addSubview:_imageView];
    [self addSubview:_infoLabel];
}
- (void)layoutSubviews{
    CGSize size = _imageView.animationImages.firstObject.size;
    _imageView.frame = CGRectMake(self.superview.center.x - size.width / 2.0, self.superview.center.y - size.height / 2.0, size.width, size.height);
    _infoLabel.frame = CGRectMake(_imageView.frame.origin.x, _imageView.frame.origin.y + size.height, size.width, 20);
}

- (void)startAnimation{
    _imageView.animationDuration = 1.0;
    [_imageView startAnimating];
    self.layer.hidden = NO;
}

- (void)stopAnimation{
    [_imageView stopAnimating];
    self.layer.hidden = YES;
}
@end
